import {
  getSaleTable,
  getSaleView
  // editSaleRecord
} from '../../api/sale'
import {
  SALE_LIST,
  SALE_CURRENT_ROW,
  SALE_DATA_COUNT,
  SALE_HISTORY_DATA,
  SALE_PAGE_SIZE,
  SALE_SUM_DATA,
  SALE_VIEW
} from '../motations-type'

export default {
  state: {
    saleList: [],
    saleSumData: [],
    saleView: [],
    saleDataCount: null,
    salePageSize: 20,
    saleHistoryData: [],
    row: {
      ctime: '',
      inviteid: '',
      mname: '',
      sex: '',
      tel: '',
      born: '',
      coludname: '',
      hisprice: '',
      mprice: '',
      hiscomm: '',
      acn: '',
      cnum: '',
      onum: ''
    }
  },
  getters: {
    saleList: state => state.saleList
  },
  mutations: {
    [SALE_LIST] (state, data) {
      state.saleList = data
    },
    [SALE_VIEW] (state, data) {
      state.saleView = data
    },
    [SALE_SUM_DATA] (state, data) {
      state.saleSumData = data
    },
    [SALE_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [SALE_DATA_COUNT] (state, data) {
      state.saleDataCount = data
    },
    [SALE_HISTORY_DATA] (state, data) {
      state.saleHistoryData = data
    },
    [SALE_PAGE_SIZE] (state, data) {
      state.salePageSize = data
    }
  },
  actions: {
    async getSaleList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let newSumData = []
      let sumCloudSale = 0
      let sumSaleMoney = 0
      let sumMonthSaleMoney = 0
      let sumCustomerCount = 0
      let sumInviteCount = 0
      let sumSignCount = 0
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSaleTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          inviteid: item.inviteid,
          ctime: item.ctime,
          mname: item.mname,
          sex: item.sex,
          tel: item.tel,
          born: item.born,
          coludname: item.coludname,
          hisprice: item.hisprice,
          mprice: item.mprice,
          hiscomm: item.hiscomm,
          acn: item.acn,
          cnum: item.cnum,
          onum: item.onum
        }]
      })
      commit(SALE_LIST, newData)
      commit(SALE_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.sale.salePageSize) {
        commit(SALE_HISTORY_DATA, newData)
      } else {
        commit(SALE_HISTORY_DATA, newData.slice(0, rootState.sale.salePageSize))
      }
      return resp
    },
    // async editSaleData ({ commit, rootState }, playload) {
    //   let data = {}
    //   if (Object.keys(playload).length > 0) {
    //     data = playload
    //   }
    //   await editSaleRecord(data)
    // },
    async getSaleCurrentRow ({ commit, rootState }, row) {
      commit(SALE_CURRENT_ROW, row)
    },
    async changeSalePage ({ commit, rootState }, index) {
      let data = rootState.sale.saleList
      let _start = (index - 1) * rootState.sale.salePageSize
      let _end = index * rootState.sale.salePageSize
      commit(SALE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeSalePageSize ({ commit, rootState }, value) {
      commit(SALE_PAGE_SIZE, value)
    },
    async getSaleViewData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSaleView(data)
      // console.log(resp)
      commit(SALE_VIEW, resp.data.data)
    }
  }
}
